import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.StringTokenizer;

public class RO_ReadASCII {
	static String FILE_PATH = "../track1/trainIdx1.txt";
//	static String FILE_PATH = "../track1/validationIdx1.txt";
//	static String FILE_PATH = "../track1/testIdx1.txt";

	static User[] USER = new User[1000990];

	public static void main(String[] args) {
		tryRead();

		if (FILE_PATH.contains("train")) {
			//sortUSER();
		}

		writeSerialisedResult();
	}

	static void tryRead() {
		System.out.println("tryRead: " + FILE_PATH);
		long time = System.currentTimeMillis();

		try {
			if (FILE_PATH.contains("test")) {
				readTest();
			} else {
				read();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		System.out.println(System.currentTimeMillis() - time);
	}

	private static void read() throws Exception {
		File file = new File(FILE_PATH);
		BufferedReader bufRdr = new BufferedReader(new FileReader(file));
		String line = null;
		StringTokenizer st;
		int userID = -1;
		int ilen = -1;
		int itemIndex = -1;

		while ((line = bufRdr.readLine()) != null) {
			if (line.contains("|")) {
				st = new StringTokenizer(line, "|");
				userID = Integer.valueOf(st.nextToken());
				ilen = Integer.valueOf(st.nextToken());

				USER[userID] = new User(ilen);
				itemIndex = 0;
			} else {
				st = new StringTokenizer(line, "\t");
				int itemID = Integer.valueOf(st.nextToken());
				byte vote = Byte.valueOf(st.nextToken());
				USER[userID].items[itemIndex] = itemID;
				USER[userID].votes[itemIndex] = vote;
				USER[userID].idToIdx.put(Integer.valueOf(itemID), Integer.valueOf(itemID));
				itemIndex++;
			}
		}
		bufRdr.close();
	}

	private static void readTest() throws Exception {
		File file = new File(FILE_PATH);
		BufferedReader bufRdr = new BufferedReader(new FileReader(file));
		String line = null;
		StringTokenizer st;
		int userID = -1;
		int ilen = -1;
		int itemIndex = -1;

		while ((line = bufRdr.readLine()) != null) {
			if (line.contains("|")) {
				st = new StringTokenizer(line, "|");
				userID = Integer.valueOf(st.nextToken());
				ilen = Integer.valueOf(st.nextToken());

				USER[userID] = new User(ilen);
				itemIndex = 0;
			} else {
				st = new StringTokenizer(line, "\t");
				int itemID = Integer.valueOf(st.nextToken());
				USER[userID].items[itemIndex] = itemID;
				USER[userID].votes[itemIndex] = 0;
				itemIndex++;
			}
		}
		bufRdr.close();
	}

	static void sortUSER() {
		System.out.println("sortUSER");
		long time = System.currentTimeMillis();

		for (User user : USER) {
			user.sort();
		}

		System.out.println(System.currentTimeMillis() - time);
	}

	private static void writeSerialisedResult() {
		System.out.println("writeSerialisedResult: " + FILE_PATH + ".ser");
		long time = System.currentTimeMillis();

		try {
			OutputStream file = new FileOutputStream(FILE_PATH + ".ser");
			OutputStream buffer = new BufferedOutputStream(file);
			ObjectOutput output = new ObjectOutputStream(buffer);
			try {
				output.writeObject(USER);
			} finally {
				output.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		System.out.println(System.currentTimeMillis() - time);
	}
}
